/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JElternAnsicht2.java
 *
 * Created on 28.04.2011, 08:44:30
 */

package gui;

import groupmanagement.GroupManagement;
import groupmanagement.IGroupManagement;
import groupmanagement.IGruppe;
import groupmanagement.Status;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import partnermanagement.IKind;
import partnermanagement.IPartnerManagement;
import partnermanagement.Kind;
import partnermanagement.PartnerManagement;
import superkigav.shared.rightsmanagement.UserTicket;

/**
 *
 * @author aas870
 */
public class JElternAnsicht extends javax.swing.JFrame {

    protected UserTicket userTicket;
    protected IPartnerManagement partnerManagement;
    protected IGroupManagement groupManagement;

    protected void updateKinder() {
        List<IKind> kinder = partnerManagement.getKinder(userTicket);
        jKindListe.setModel(new DefaultComboBoxModel(kinder.toArray()));
    }

    protected void updateGruppen() {
        List<IGruppe> gruppen = groupManagement.getGruppen(userTicket);
        jGruppenListe.setModel(new DefaultComboBoxModel(gruppen.toArray()));
    }

    protected void updateSelection() {
        if (jKindListe.getModel().getSelectedItem() != null) {
            Kind kind = (Kind)jKindListe.getModel().getSelectedItem();
            List<IGruppe> gruppen = groupManagement.getGruppen(kind.getId(), userTicket);
            String[][] anmeldungen = new String[gruppen.size()][2];
            String[] header = {"Name:", "Status:"};
            int counter = 0;

            for (IGruppe g : gruppen) {
                // In allen Gruppen durchgehen, ob Kind dort aufgenommen oder
                // in Warteliste ist und entsprechend:
                // Gruppenname, "Wartelist" / "Aufgenommen" als Table ausgeben
                anmeldungen[counter][0] = g.getName();
                
                if (groupManagement.getStatus(kind.getId(), g, userTicket) == Status.WARTELISTE) {
                    anmeldungen[counter][1] = "Warteliste";
                } else {
                    // sonst wäre Gruppe nicht in der Liste
                    anmeldungen[counter][1] = "Aufgenommen";
                }
                counter++;
            }

            jAnmeldungen.setModel(new DefaultTableModel(anmeldungen, header));
        }
    }

    /** Creates new form JElternAnsicht2 */
    public JElternAnsicht(UserTicket userTicket) {
        this.userTicket = userTicket;
        this.partnerManagement = new PartnerManagement();
        this.groupManagement = new GroupManagement();

        initComponents();
        this.updateKinder();
        this.updateGruppen();
        this.updateSelection();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jAnmeldungen = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jGruppenListe = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jKindListe = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jFruehdienst = new javax.swing.JCheckBox();
        jSpaetdienst = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jAnmeldungen.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Gruppenname:", "Status"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jAnmeldungen);

        jLabel3.setText("Das Kind ist für folgende Gruppen angemeldet oder auf der Warteliste:");

        jGruppenListe.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel2.setText("Gruppe:");

        jLabel1.setText("Kind:");

        jKindListe.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jKindListe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                update(evt);
            }
        });

        jButton1.setText("Neues Kind anlegen");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Anmelden");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jFruehdienst.setText("Frühdienst");

        jSpaetdienst.setText("Spätdienst");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jKindListe, 0, 205, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jLabel3)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jFruehdienst)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jSpaetdienst)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 104, Short.MAX_VALUE)
                                .addComponent(jButton2))
                            .addComponent(jGruppenListe, 0, 337, Short.MAX_VALUE)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jKindListe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jGruppenListe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jFruehdienst)
                    .addComponent(jSpaetdienst)
                    .addComponent(jButton2))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if (jKindListe.getModel().getSelectedItem() == null
                || jGruppenListe.getModel().getSelectedItem() == null) {
            JOptionPane.showMessageDialog(null, "Bitte wählen Sie ein Kind und eine Gruppe aus!", "Fehler:", JOptionPane.OK_OPTION);
        } else {
            Kind kind = (Kind)jKindListe.getModel().getSelectedItem();
            IGruppe gruppe = (IGruppe)jGruppenListe.getModel().getSelectedItem();
            try {
                groupManagement.addKindToGruppe(kind.getId(), gruppe, jFruehdienst.isSelected(), jSpaetdienst.isSelected(), userTicket);
            } catch (Exception e) {
                // Weitere Exceptions möglich, wie kann nicht mehr eingetragen werden oder sonst was...
                JOptionPane.showMessageDialog(null, "Das Kind wurde bereits für diese Gruppe eingetragen!", "Fehler:", JOptionPane.OK_OPTION);
            }
        }
        updateSelection();
}//GEN-LAST:event_jButton2ActionPerformed

    private void update(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_update
        updateSelection();
    }//GEN-LAST:event_update

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable jAnmeldungen;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jFruehdienst;
    private javax.swing.JComboBox jGruppenListe;
    private javax.swing.JComboBox jKindListe;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JCheckBox jSpaetdienst;
    // End of variables declaration//GEN-END:variables

}
